package com.whity.towerdefense.controler.states;

import java.util.Vector;

import com.whity.towerdefense.R;
import com.whity.towerdefense.TowerDefenseActivity;
import com.whity.towerdefense.controler.GameControler;
import com.whity.towerdefense.controler.I_State;
import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.model.Level;
import com.whity.towerdefense.model.TowerFactory;
import com.whity.towerdefense.model.TowerType;
import com.whity.towerdefense.view.ActionPhaseLayout;
import com.whity.towerdefense.view.GraphicElt;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class IdleAP implements I_State
{
	// =============================================================
	// Public attributs
	
	public static boolean	FIRST_RUN	= true;
	
	// =============================================================
	// Private attributs
	
	private static final String		TAG = "com.djm.test03.controler.states";
		
	private GraphicElt				m_MapGraphicElt;
	private	SlidingDrawer			m_TowerPanel;
	private ImageView				m_TowerHandle;
	
	// =============================================================
	// Public functions :
	
	public IdleAP()
	{
		m_MapGraphicElt = GameModel.LEVEL.GetMap().GetGraphic();
		m_TowerPanel = (SlidingDrawer)TowerDefenseActivity.ACTIVITY.findViewById(R.id.TowerSlider);
		m_TowerHandle = (ImageView)TowerDefenseActivity.ACTIVITY.findViewById(R.id.handle);
		
		m_TowerPanel.bringToFront();
	}
	
	public void OnChangeState()
	{
		if(FIRST_RUN)
		{
			FIRST_RUN = false;
			m_MapGraphicElt.SetScale((float)(m_MapGraphicElt.GetScale() * 0.001));
		}
	}
	
	public void OnClickEvent(View p_View)
	{
		if(p_View.getId() == R.id.StartWaveLabel)
		{
			GameControler.GAME_MODEL.StartLevel();
			p_View.setVisibility(View.GONE);
			
		}
	}
	
	public void OnDrawerOpenEvent()
	{
		m_TowerPanel.bringToFront();
		m_TowerHandle.setImageResource(R.drawable.right_arrow);
	}
	
	public void OnDrawerCloseEvent()
	{
		m_TowerPanel.bringToFront();
		m_TowerHandle.setImageResource(R.drawable.left_arrow);
	}
	
	public void OnPause()
	{
		// Pause the game :
		GameControler.GAME_MODEL.Pause();
		
		// TODO : Save the game
	}
	
	public void OnResume()
	{
		// Pause the game :
		GameControler.GAME_MODEL.Resume();
		
		// TODO : Save the game
	}
	
	public boolean OnMotionEvent(View p_View, MotionEvent p_Evt)
	{
		if(p_View.getId() == R.id.Tower01 || p_View.getId() == R.id.Tower02 || p_View.getId() == R.id.Tower03 || p_View.getId() == R.id.Tower04)
		{
			if(p_Evt.getAction() == MotionEvent.ACTION_DOWN)
			{
				TowerType towerType = null;
				Vector<String> types = GameModel.LEVEL.GetBaseTowers();
				
				switch(p_View.getId())
				{
					case R.id.Tower01:
					{
						towerType = TowerFactory.GetTowerType(types.elementAt(0));
					}
					break;
					case R.id.Tower02:
					{
						towerType = TowerFactory.GetTowerType(types.elementAt(1));
					}
					break;
					case R.id.Tower03:
					{
						towerType = TowerFactory.GetTowerType(types.elementAt(2));
					}
					break;
					case R.id.Tower04:
					{
						towerType = TowerFactory.GetTowerType(types.elementAt(3));
					}
					break;
					default:
					{
						towerType = TowerFactory.GetTowerType(types.elementAt(0));
					}
					break;
				}
				GameControler.SetState(new DragNewTowerAP(towerType));
			}
			return true;
		}
		else if(p_View.getId() == R.id.Action_phase_view)
		{
			if(p_Evt.getAction() == MotionEvent.ACTION_DOWN)
			{
				GameControler.SetState(new DragMapAP(p_Evt.getRawX(), p_Evt.getRawY()));
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			Log.w(TAG, "IdleAP: Motion evt not handled, view ID: " + p_View.getId());
			return false;
		}
	}
	
	// =============================================================
}
